package com.tianzeculture.stagebao.activity.home.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.business.BusinessActivity;
import com.tianzeculture.stagebao.activity.business.NoticeDetailActivity;
import com.tianzeculture.stagebao.activity.home.adapter.NoticeAdapter;
import com.tianzeculture.stagebao.activity.home.popwindow.NoticeSearchPopWindow;
import com.tianzeculture.stagebao.activity.home.popwindow.NoticeTypePopWindow;
import com.tianzeculture.stagebao.activity.home.popwindow.PrefessionTypePopWindow;
import com.tianzeculture.stagebao.activity.home.popwindow.SelectCityPopWindow;
import com.tianzeculture.stagebao.base.BaseFragment;
import com.tianzeculture.stagebao.bean.AllIndustryBean;
import com.tianzeculture.stagebao.bean.JobListBean;
import com.tianzeculture.stagebao.bean.Notice;
import com.tianzeculture.stagebao.bean.NoticeList;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.net.BusinessRestUsage;
import com.tianzeculture.stagebao.net.CommonRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.util.Util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.tianzeculture.stagebao.util.Util.PAGE_SIZE;

/**
 * Created by Administrator on 2018/5/29 0029.
 * 招募
 */

public class NoticeFragment extends BaseFragment
        implements SelectCityPopWindow.SelectCityListner,
        PrefessionTypePopWindow.NoticeTypeListener,
        NoticeSearchPopWindow.NoticeSearchListener {

    String cityName = "";
    @BindView(R.id.edtSearch)
    EditText edtSearch;

    @BindView(R.id.lvNotice)
    ListView lvNotice;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    NoticeAdapter noticeAdapter;

    @BindView(R.id.boxCity)
    CheckBox boxCity;
    @BindView(R.id.boxStyle)
    CheckBox boxStyle;
    @BindView(R.id.boxSearch)
    CheckBox boxSearch;

    @Request
    BusinessRestUsage businessRestUsage = null;
    @Request
    CommonRestUsage commonRestUsage = null;
    private int toWorker = 101;//获取招募列表
    private int industryListId = 102;//获取职业
    private Notice notice = new Notice();
    String searchString = "";
    private int pager = 1;
    List<NoticeList> list = new ArrayList<>();
    List<AllIndustryBean> allJobList;


    @Override
    public void initUI() {
        super.initUI();
        commonRestUsage.industryList(industryListId);
        edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    notice.setContent(edtSearch.getText().toString().trim());
                    pager = 1;
                    businessRestUsage.toWorker(toWorker, notice, pager, PAGE_SIZE);
                }
                return false;
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pager = 1;
                        businessRestUsage.toWorker(toWorker, notice, pager, PAGE_SIZE);
                        refreshlayout.finishRefresh();
//                        refreshlayout.setNoMoreData(false);
                    }
                }, 2000);

            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pager++;
                        businessRestUsage.toWorker(toWorker, notice, pager, PAGE_SIZE);
                        refreshLayout.finishLoadMore();
                    }
                }, 2000);
            }
        });

        noticeAdapter = new NoticeAdapter(getContext(), list, handler);
        lvNotice.setAdapter(noticeAdapter);

        lvNotice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), NoticeDetailActivity.class);
                intent.putExtra("notice", (Serializable) list.get(i));
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
//        initNotice(noticeLists);
        showProgressDialog();
        pager = 1;
        businessRestUsage.toWorker(toWorker, notice, pager, PAGE_SIZE);
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == toWorker) {
            dismissProgressDialog();
            if (msg.getObj() != null) {
                JobListBean resultBean = (JobListBean) msg.getObj();
                if (pager == 1)
                    list.clear();
                list.addAll(resultBean.getMyResult());
                noticeAdapter.notifyDataSetChanged();
            }
        }


        if (taskId == industryListId) {
            if (msg.getData()) {
                if (msg.getObj() == null)
                    return;
                allJobList = ((ResultBean) msg.getObj()).getIndustry();
            }
        }
    }


    @OnClick(R.id.boxCity)
    public void showCityPopWindow(View view) {
        //判断软键盘是否打开，为true则关闭，否则不处理
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            Util.closeKeyboard(getActivity());
        }
        SelectCityPopWindow mPopWindow = new SelectCityPopWindow(getActivity(), cityName);
        mPopWindow.setSelectCityListener(this);
        mPopWindow.showPopupWindow(boxStyle);
    }

    @OnClick(R.id.boxStyle)
    public void showStylePopWindow(View view) {
        //判断软键盘是否打开，为true则关闭，否则不处理
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            Util.closeKeyboard(getActivity());
        }

        PrefessionTypePopWindow mPopWindow = new PrefessionTypePopWindow(getActivity(), allJobList);
        mPopWindow.setNoticeTypeListener(this);
        mPopWindow.showPopupWindow(boxStyle);
    }

    @OnClick(R.id.boxSearch)
    public void showSearchPopWindow(View view) {
        //判断软键盘是否打开，为true则关闭，否则不处理
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        if (isOpen) {
            Util.closeKeyboard(getActivity());
        }

        NoticeSearchPopWindow mPopWindow = new NoticeSearchPopWindow(getActivity());
        mPopWindow.setNoticeSearchListener(this);
        mPopWindow.showPopupWindow(boxSearch);
    }


    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_notice;
    }

    @Override
    public void pickSelectCityValue(String value) {
        this.cityName = value;
        boxCity.setText(value);
        if ("全国".equals(value) || "不限".equals(value)) {
            notice.setAddress("");
        } else
            notice.setAddress(value);
        pager = 1;
        businessRestUsage.toWorker(toWorker, notice, pager, PAGE_SIZE);
    }

    @Override
    public void selectCityCannel() {
        boxCity.setChecked(false);
    }

    @Override
    public void pickNoticeTypeValue(String value) {
        boxStyle.setText(value);
        if ("不限".equals(value))
            notice.setWorkType("");
        else notice.setWorkType(value);
        boxStyle.setChecked(false);
        pager = 1;
        businessRestUsage.toWorker(toWorker, notice, pager, PAGE_SIZE);
    }

    @Override
    public void noticeTypeCancel() {
        boxStyle.setChecked(false);
    }

    @Override
    public void pickNoticeSearchValue(Notice noti) {
        notice.setAddTime(noti.getAddTime() == null ? "" : noti.getAddTime());
        notice.setStratTime(noti.getStratTime() == null ? "" : noti.getStratTime());
        notice.setEndTime(noti.getEndTime() == null ? "" : noti.getEndTime());
        notice.setIstravel(noti.getIstravel() == null ? "" : noti.getIstravel());
        notice.setIscostume(noti.getIscostume() == null ? "" : noti.getIscostume());
        notice.setSex(noti.getSex() == null ? "" : noti.getSex());
        pager = 1;
        businessRestUsage.toWorker(toWorker, notice, pager, PAGE_SIZE);
    }

    @Override
    public void noticeSearchCancel() {
        boxSearch.setChecked(false);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 6:
                    Bundle bundle = msg.getData();
                    Intent intent = new Intent(getContext(), BusinessActivity.class);
                    intent.putExtra("userId", bundle.getString("userId"));
                    startActivity(intent);
                    break;
            }
        }
    };
}
